#!/bin/sh -eu

. "$(dirname "$0")/common.sh"

test_cleanup() {
    if mountpoint -q "$scratch_dir/mnt"; then
	umount "$scratch_dir/mnt"
    fi
}

assert_kconfig CONFIG_F2FS_FS
assert_kconfig CONFIG_F2FS_FS_ENCRYPTION

curl -o "$scratch_dir/final.img.zip" \
     "https://bugzilla.kernel.org/attachment.cgi?id=277309"
unzip -d "$scratch_dir" "$scratch_dir/final.img.zip" final.img

# Proof-of-concept code is inline in a comment :-(
curl -o "$scratch_dir/comments.json" \
     "https://bugzilla.kernel.org/rest/bug/comment/1292063"
jq --raw-output '.comments["1292063"].text' "$scratch_dir/comments.json" \
    | sed -n '/^- POC/,/^- kernel message/ { /^-/d; p }' \
    > "$scratch_dir/poc.c"
cc -Wall -Wextra -O2 "$scratch_dir/poc.c" -o "$scratch_dir/poc"

mkdir "$scratch_dir/mnt"
mount -o loop -t f2fs "$scratch_dir/final.img" "$scratch_dir/mnt"

assert_untainted

"$scratch_dir/poc" "$scratch_dir/mnt"

assert_untainted $((0x200))  # expect and ignore TAINT_WARN
